package common;

import java.util.*;

/**
 * @Description
 * @auther lileilei
 * @create 2020-01-06 13:21
 */
//组合参
public class CombinationParameters {
    public Map<String, Object> combinationparame(String paramelist, String key,
                                                 String commonParamedetaiVoList) {
        RandomFunction randomFunction = new RandomFunction();
        Map<String, Object> resultmap = new HashMap<>();
        List paramelistMap = new ArrayList<>();
        GetComplexJson getComplexJson = new GetComplexJson();
        paramelistMap = Arrays.asList(paramelist);
        RedisOpearting redisOpearting = new RedisOpearting();
        List commonlist = Arrays.asList(commonParamedetaiVoList);
        for (Object o : paramelistMap) {
            try {
                Map<String, String> map = (Map<String, String>) o;
                if (map.get("types").equals("str")) {
                    if (map.get("parames").startsWith("#")) {
                        String name = map.get("parames").split("#")[1];
                        String[] value = name.split(".");
                        String commonname = value[0];
                        String commonkey = value[1];
                        for (Object item : commonlist) {
                            Map<String, Object> stringObjectMap = (Map<String, Object>) item;
                            if (stringObjectMap.get("name").equals(commonname)) {
                                String name_selt = stringObjectMap.get("detail").toString();
                                StringJsonParson stringJsonParson = new StringJsonParson();
                                resultmap.put(map.get("feilds"), stringJsonParson.json2map(name_selt).get(commonkey));
                            }
                        }
                    } else if (map.get("parames").startsWith("@")) {
                        String getCaseResultKey = null;
                        String getCaseValueKey = null;
                        Integer index = 0;
                        if (map.get("parames").contains("=")) {
                            String key_map = map.get("parames").split("@")[0];
                            getCaseResultKey = key + "_" + key_map;
                            getCaseValueKey = map.get("parames").split("@")[1].split(".")[1];
                            try {
                                index = Integer.valueOf(getCaseValueKey.split("=")[1]) - 1;
                            } catch (NumberFormatException e) {
                                index = 0;
                            }

                            resultmap.put(map.get("feilds"), getComplexJson.getValueList(redisOpearting.getkey(getCaseResultKey).toString(), map.get("parameconent").split("@")[1].split(".")[0]).get(index));
                        }

                        //依赖用例
                    } else if (map.get("parames").startsWith("__date")) {
                        //yyyy-MM-dd
                        if (map.get("parames").contains("-")) {
                            String[] list = map.get("parames").split("-");
                            if (list.length <= 0) {
                                resultmap.put(map.get("feilds"), randomFunction.get_time_less(1));
                            } else {
                                try {
                                    Integer add = new Integer(list[1]);
                                    resultmap.put(map.get("feilds"), randomFunction.get_time_less(add));
                                } catch (NumberFormatException e) {
                                    resultmap.put(map.get("feilds"), randomFunction.get_time_less(1));
                                }
                            }
                        } else if (map.get("parames").contains("+")) {
                            String[] list = map.get("parames").split("\\+");
                            if (list.length <= 0) {
                                resultmap.put(map.get("feilds"), randomFunction.get_time_add(1));
                            } else {
                                try {
                                    Integer add = new Integer(list[1]);
                                    resultmap.put(map.get("feilds"), randomFunction.get_time_add(add));
                                } catch (NumberFormatException e) {
                                    resultmap.put(map.get("feilds"), randomFunction.get_time_add(1));
                                }
                            }
                        } else {
                            resultmap.put(map.get("feilds"), randomFunction.get_time_format());
                        }
                    } else if (map.get("parames").startsWith("__datelogng")) {
                        //long 类型
                        resultmap.put(map.get("feilds"), randomFunction.gettime());
                    } else if (map.get("parames").startsWith("__datenew")) {
                        //yyyy-MM-dd HH:mm:ss
                        if (map.get("parames").contains("-")) {
                            String[] list = map.get("parames").split("-");
                            if (list.length <= 0) {
                                resultmap.put(map.get("feilds"), randomFunction.get_time_format_new_less(1));
                            } else {
                                try {
                                    Integer add = new Integer(list[1]);
                                    resultmap.put(map.get("feilds"), randomFunction.get_time_format_new_less(add));
                                } catch (NumberFormatException e) {
                                    resultmap.put(map.get("feilds"), randomFunction.get_time_format_new_less(1));
                                }
                            }
                        } else if (map.get("parames").contains("+")) {
                            String[] list = map.get("parames").split("\\+");
                            if (list.length <= 0) {
                                resultmap.put(map.get("feilds"), randomFunction.get_time_format_new_add(1));
                            } else {
                                try {
                                    Integer add = new Integer(list[1]);
                                    resultmap.put(map.get("feilds"), randomFunction.get_time_format_new_add(add));
                                } catch (NumberFormatException e) {
                                    resultmap.put(map.get("feilds"), randomFunction.get_time_format_new_add(1));
                                }
                            }
                        } else {
                            resultmap.put(map.get("feilds"), randomFunction.get_time_format_new());
                        }
                    } else {
                        resultmap.put(map.get("feilds"), map.get("parames"));
                    }
                } else if (map.get("types").equals("long")) {
                    if (map.get("parames").startsWith("#")) {
                        String name = map.get("parames").split("#")[1];
                        String[] value = name.split(".");
                        String commonname = value[0];
                        String commonkey = value[1];
                        for (Object item : commonlist) {
                            Map<String, Object> stringObjectMap = (Map<String, Object>) item;
                            if (stringObjectMap.get("name").equals(commonname)) {
                                String name_selt = stringObjectMap.get("detail").toString();
                                StringJsonParson stringJsonParson = new StringJsonParson();
                                resultmap.put(map.get("feilds"), stringJsonParson.json2map(name_selt).get(commonkey));
                            }
                        }
                        //依赖参数
                    } else if (map.get("parames").startsWith("@")) {
                        String getCaseResultKey = null;
                        String getCaseValueKey = null;
                        Integer index = 0;
                        if (map.get("parames").contains("=")) {
                            String key_map = map.get("parames").split("@")[0];
                            getCaseResultKey = key + "_" + key_map;
                            getCaseValueKey = map.get("parames").split("@")[1].split(".")[1];
                            try {
                                index = Integer.valueOf(getCaseValueKey.split("=")[1]) - 1;
                            } catch (NumberFormatException e) {
                                index = 0;
                            };
                            resultmap.put(map.get("feilds"), getComplexJson.getValueList(redisOpearting.getkey(getCaseResultKey).toString(), map.get("parames").split("@")[1].split(".")[0]).get(index));
                        }

                    } else {
                        resultmap.put(map.get("feilds"), map.get("parameconent"));
                    }

                } else if (map.get("types").equals("int")) {
                    if (map.get("parames").startsWith("#")) {
                        String name = map.get("parames").split("#")[1];
                        String[] value = name.split(".");
                        String commonname = value[0];
                        String commonkey = value[1];
                        for (Object item : commonlist) {
                            Map<String, Object> stringObjectMap = (Map<String, Object>) item;
                            if (stringObjectMap.get("name").equals(commonname)) {
                                String name_selt = stringObjectMap.get("detail").toString();
                                StringJsonParson stringJsonParson = new StringJsonParson();
                                try {
                                    resultmap.put(map.get("feilds"), new Integer((Integer) stringJsonParson.json2map(name_selt).get(commonkey)));
                                } catch (Exception e) {

                                }
                            }
                        }
                        //依赖参数
                    } else if (map.get("parames").startsWith("@")) {
                        String getCaseResultKey = null;
                        String getCaseValueKey = null;
                        Integer index = 0;
                        if (map.get("parames").contains("=")) {
                            String key_map = map.get("parames").split("@")[0];
                            getCaseResultKey = key + "_" + key_map;
                            getCaseValueKey = map.get("parames").split("@")[1].split(".")[1];
                            try {
                                index = Integer.valueOf(getCaseValueKey.split("=")[1]) - 1;
                            } catch (NumberFormatException e) {
                                index = 0;
                            }
                            try {
                                resultmap.put(map.get("feilds"), ((Integer) getComplexJson.getValueList(redisOpearting.getkey(getCaseResultKey).toString(), map.get("parames").split("@")[1].split(".")[0]).get(index)));
                            } catch (Exception e) {

                            }
                        }
                    } else {
                        try {
                            resultmap.put(map.get("feilds"), (new Integer(map.get("parames"))));
                        } catch (Exception e) {

                        }
                    }

                } else if (map.get("types").equals("list")) {
                    if (map.get("parames").startsWith("#")) {
                        List d = new ArrayList();
                        //依赖参数
                        String name = map.get("parames").split("#")[1];
                        String[] value = name.split(".");
                        String commonname = value[0];
                        String commonkey = value[1];
                        for (Object item : commonlist) {
                            Map<String, Object> stringObjectMap = (Map<String, Object>) item;
                            if (stringObjectMap.get("name").equals(commonname)) {
                                String name_selt = stringObjectMap.get("detail").toString();
                                StringJsonParson stringJsonParson = new StringJsonParson();
                                try {
                                    d = (List) stringJsonParson.json2map(name_selt).get(commonkey);
                                    resultmap.put(map.get("feilds"), d);
                                } catch (Exception e) {

                                }
                            }
                        }
                    } else if (map.get("parames").startsWith("@")) {
                        //依赖用例
                        List d_result = new ArrayList();
                        String getCaseResultKey = null;
                        String getCaseValueKey = null;
                        Integer index = 0;
                        if (map.get("parames").contains("=")) {
                            String key_map = map.get("parames").split("@")[0];
                            getCaseResultKey = key + "_" + key_map;
                            getCaseValueKey = map.get("parames").split("@")[1].split(".")[1];
                            try {
                                index = Integer.valueOf(getCaseValueKey.split("=")[1]) - 1;
                            } catch (NumberFormatException e) {
                                index = 0;
                            }
                            try {
                                d_result = (List) getComplexJson.getValueList(redisOpearting.getkey(getCaseResultKey).toString(), map.get("parameconent").split("@")[1].split(".")[0]).get(index);
                                resultmap.put(map.get("feilds"), d_result);
                            } catch (Exception e) {

                            }
                        }
                    } else {
                        resultmap.put(map.get("feilds"), map.get("parames"));
                    }
                } else {
                    resultmap.put(map.get("feilds"), map.get("parames"));
                }
            } catch (Exception e) {

            }

        }
        return resultmap;
    }
}